package com.zkingsoft.actions.admin.distribution;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zkingsoft.anotations.RemoveRequestToken;
import com.zkingsoft.anotations.SaveRequestToken;
import com.zkingsoft.authority.MatrixAdapter;
import com.zkingsoft.constraint.BaseController;
import com.zkingsoft.model.distribution.DisDesigner;
import com.zkingsoft.pojo.AjaxResult;
import com.zkingsoft.pojo.PaginationVO;
import com.zkingsoft.services.distribution.DisDesignerService;
import com.zkingsoft.util.EncrypUtil;
import com.zkingsoft.util.StringUtils;
import com.zkingsoft.util.WebUtil;

/**
 * This field was generated by Zking.software.Codegen.
 * 
 * @date 2017-06-26 07:15
 */
@Controller
@RequestMapping(value = "admin/disDesigner")
public class DisDesignerController extends BaseController {

	@Resource
	private DisDesignerService disDesignerService;

	@Resource
	private MatrixAdapter matrixAdapter;

	// 记录编辑前的值Before_Edit_Value
	public static final String BEV = "DisDesigner_BEV";

	public static final String fnCode = "DisDesigner";
	public static final String search = fnCode + ":search";
	public static final String edit = fnCode + ":edit";
	public static final String del = fnCode + ":del";
	public static final String add = fnCode + ":add";

	/**
	 * 列表显示
	 */
	@RequestMapping(value = "/showList")
	public @ResponseBody AjaxResult showList(DisDesigner disDesigner,
			PaginationVO pageVo) {
		matrixAdapter.isBtnPermitted(search);
		return showList(disDesignerService, disDesigner, pageVo);
	}

	/**
	 * 新增或者修改页面
	 */
	@RemoveRequestToken
	@RequestMapping(value = "/addOrModify")
	public @ResponseBody AjaxResult addOrModify(DisDesigner disDesigner) {
		if (disDesigner.getDesId() != null) {
			matrixAdapter.isBtnPermitted(edit);
			disDesigner.setDesId(((DisDesigner) WebUtil
					.getSessionAttribute(BEV)).getDesId());
			AjaxResult result = modify(disDesignerService,
					WebUtil.getSessionAttribute(BEV), disDesigner, "设计师");
			WebUtil.removeSessionAttribute(BEV);
			return result;
		} else {
			matrixAdapter.isBtnPermitted(add);
			
			try {
				disDesigner.setDesPassword(EncrypUtil.getMD5(disDesigner.getDesPassword()));
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			disDesigner.setDesInviteCode(StringUtils.getRandomString(10));
			disDesigner.setDesRebate(new BigDecimal(0));
			disDesigner.setDesRegisterTime(new Date());
			disDesigner.setDesAuditTime(new Date());
			
			return add(disDesignerService, disDesigner, "设计师");
		}
	}

	/**
	 * 进入修改界面
	 */
	@SaveRequestToken
	@RequestMapping(value = "/editForm")
	public String editForm(Long id) {
		matrixAdapter.isBtnPermitted(edit);
		DisDesigner disDesigner;
		if (id != null) {
			disDesigner = disDesignerService.findById(id);
			WebUtil.getRequest().setAttribute("obj", disDesigner);
			WebUtil.setSessionAttribute(BEV, disDesigner);
		}
		return "admin/distribution/DisDesigner-form";
	}

	/**
	 * 删除
	 */
	@RequestMapping(value = "/del")
	public @ResponseBody AjaxResult del(String keys) {
		matrixAdapter.isBtnPermitted(del);
		return remove(disDesignerService, keys);
	}

	
	/**
	 * 设计师下拉框显示
	 */
	@RequestMapping(value = "/showAll")
	public @ResponseBody AjaxResult showAll(DisDesigner disDesigner) {
		matrixAdapter.isBtnPermitted(search);
		List<DisDesigner> dataList = disDesignerService.findByModel(disDesigner);
		AjaxResult result = new AjaxResult(AjaxResult.STATUS_OK, dataList);
		return result;
	}
	
}